<?php
class google extends command {

	public function __construct(){
		$this->name = 'google';
		$this->public = true;
	}
	
	public function help(){
		return "Uso: !google <criterio de búsqueda> . Devuelve los 3 primeros resultados que arroja google.";
	}
	
	public function process($args){
		$this->output = "";
		$args = trim($args);
		$args = str_replace(" ", "+",$args);
		$buffer="";
		$gestor = @fopen("http://www.google.com.mx/search?hl=es&q=" . $args . "&btnG=Buscar+con+Google&meta=&aq=f&oq=", "r");
		while (!feof($gestor)) {
			$buffer = $buffer.fgets($gestor, 4096);
		}
		fclose ($gestor);

		$buffer = str_replace("\n", "", $buffer);
		
		preg_match_all ("/<h3 class=r>(.*?)<\/h3>/", $buffer, $matches);
		$results = array_slice($matches[1], 0, 3,true);
		
		foreach ( $results as $result){
			$result = substr($result, strpos($result, 'http'));
			$result = substr($result, 0, strpos($result, '"'));
			$this->output .= $result . "\n";
		}
	}
}
?>